//
//  MainTableView.m
//  云律司
//
//  Created by 韦淋 on 2017/6/22.
//  Copyright © 2017年 test. All rights reserved.
//

#import "MainTableView.h"




@interface MainTableView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSArray *newsArray;
@property (nonatomic, strong) NSString *newsTitle;
@end

@implementation MainTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    self = [super initWithFrame:frame style:style];
    
    if (self) {
        
        self.newsArray = [NSArray array];
        [self getData];
        self.dataSource = self;
        self.delegate = self;
        self.rowHeight = (SCREEN_HEIGHT * 5 - 310) / 20;
        //下拉刷新
        
        NSMutableArray *images = [NSMutableArray array];//图片
        for (int i = 1; i < 48; i++) {//假设50张图片
            [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%zd",i]]];
        }
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        
        //        [header setImages:images forState:MJRefreshStateRefreshing];// 设置正在刷新状态的动画图片
        
        // 设置普通状态的动画图片
//        [header setImages:@[images[0]] forState:MJRefreshStateIdle];
        // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
//        [header setImages:@[images[0],images[1],images[2],images[3],images[4],images[5],images[6],images[7],images[8],images[9]] forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [header setImages:images forState:MJRefreshStateRefreshing];
        self.mj_header = header;// 设置header
        header.lastUpdatedTimeLabel.hidden = YES;// 隐藏时间
        header.stateLabel.hidden = YES;// 隐藏状态
    }
    return self;
}
- (void)getData{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [NetworkingManager getUrl:[NSString stringWithFormat:@"%@gsid=%@",NEWS_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData) {
            NSLog(@"----------%@",responseData);
            for (NSDictionary *dic  in responseData[@"result"]) {
                if ([dic[@"product_brand"] integerValue] == 14) {
                    self.newsTitle = @"公司新闻";
                }
                if ([dic[@"product_brand"] integerValue] == 15) {
                    self.newsTitle = @"司法局新闻";
                }
                if ([dic[@"product_brand"] integerValue] == 16) {
                    self.newsTitle = @"系统新闻";
                }
            }
            self.newsArray = responseData[@"result"];
            [self reloadData];
            [self endRefreshing];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络异常"];
        }];
    });
    
    
}
-(void)setContentOffsetY:(CGFloat)contentOffsetY {
    
    _contentOffsetY = contentOffsetY;
    if (![self.mj_header isRefreshing]) {
        
        self.contentOffset = CGPointMake(0, contentOffsetY);
    }
}

-(void)startRefreshing {
    [self.mj_header beginRefreshing];
}
-(void)endRefreshing {
    [self.mj_header endRefreshing];
}


#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainTableViewCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.lineView.hidden = YES;
    }
    cell.titleLable.text = self.newsTitle;
    cell.contenLable.text = [NSString stringWithFormat:@"%@",self.newsArray[indexPath.row][@"product_name"]];
    cell.msgTypeLable.text = [NSString stringWithFormat:@"消息类型： %@",self.newsTitle];
    cell.publisherLable.text = [NSString stringWithFormat:@"发布人： %@",self.newsArray[indexPath.row][@"insert_name"]];
    cell.dTimeLable.text = [self.newsArray[indexPath.row][@"insert_time"] substringWithRange:NSMakeRange(8, 2)];
    cell.mYTimeLable.text = [self.newsArray[indexPath.row][@"insert_time"] substringToIndex:7];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.bounds.size.width, 70)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.bounds.size.width, 10)];
    grayView.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10,10, self.bounds.size.width, 60)];
    lable.text = @"最新资讯";
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(5,70, self.bounds.size.width - 10, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
    
    [header addSubview:grayView];
    [header addSubview:lable];
    [header addSubview:lineView];
    return header;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.bounds.size.width, 20)];
    footer.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 20)];
    label.text = @"－－－－我是有底线的－－－－";
    label.font = [UIFont systemFontOfSize:11];
    label.textAlignment = NSTextAlignmentCenter;
    
    [footer addSubview:label];
    
    return footer;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击cell ");
//    NewsDetailViewController *NDVC= [[NewsDetailViewController alloc] init];
//    
//    NDVC.newsID = self.firmInfo[indexPath.row][@"id"];
//    
//    [self.navigationController pushViewController:NDVC animated:YES];
}


@end
